#!/usr/bin/python
"""
examples_test.py

Test that cre_examples.py is correct.  So we can confidently use them in
documentation.
"""

import unittest

import cre_examples  # data file
import annex


class ExamplesTest(unittest.TestCase):

  def testUrlRegex(self):
    e = cre_examples.GetExample('url-capture')
    original = cre_examples.ORIG_URL_REGEX
    r = annex.Regex(e['cre'], annex.DEBUG)

    # TODO: Write in test location.
    compiled = r.to_py()
    print repr(original)
    o = original+ '\n'
    open('orig.txt', 'wb').write(o)
    c = compiled.encode('utf-8') + '\n'
    open('comp.txt', 'wb').write(c)

    # NOTE: They're not exactly the same.
    return
    self.assertEqual(original, compiled)

  def testExamples(self):
    annex.testall(self, cre_examples.EXAMPLES)
    return

    for name, value in cre_examples.EXAMPLES.iteritems():
      cre, input_ = value
      py_re = None

      print name
      r = annex.Regex(cre, annex.DEBUG)

      py = r.to_py()
      print py
      import re
      p = re.compile(py)

      for in_ in input_.splitlines():
        in_ = in_.strip()
        s = r.match(in_)
        print in_, s

        #print repr(in_), p.match(in_)

  def testUnitTestMatch(self):
    annex.testall(self, [
      { 'cre': 'digit+',
        'match': [
            ('3', '3'),
            ],
        'matchspan': [
            ('3', annex.Span('3', 0, 1)),
            ],
        'nomatch': [
            'a'
            ],
      }
      ])

  def testUnitTestCapture(self):
    annex.testall(self, [
      { 'cre': '{digit+ as size}',
        'capture': [
            ('3', {'size': '3', 'MATCH': '3'}),
            ],
        'capturespans': [
            ('3', {'size': annex.Span('3', 0, 1),
                   'MATCH': annex.Span('3', 0, 1)} ),
            ],
        'nomatch': [
            'a'
            ],
      }
      ])


if __name__ == '__main__':
  unittest.main()
